package top.sdaily.web.controller.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.sdaily.web.context.ReturnBody;
import top.sdaily.web.dto.DRoleMenuTreeNode;
import top.sdaily.web.model.Role;
import top.sdaily.web.service.MenuService;
import top.sdaily.web.service.RoleMenuRightService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by TsujiShiki on 2017/1/24.
 */
@RestController
public class RoleMenuRightController {

    @Autowired
    MenuService menuService;

    @Autowired
    RoleMenuRightService roleMenuRightService;

    @RequestMapping(value = "rolemenuright", method = RequestMethod.GET)
    public ReturnBody getMenuRight() {
        List<Role> allRoleMenuRight = roleMenuRightService.getAllRoleMenuRight();
        return ReturnBody.success().setData(allRoleMenuRight);
    }

    @RequestMapping(value = "rolemenuright", method = {RequestMethod.POST, RequestMethod.PUT})
    public ReturnBody modifyRoleMenuRight(@RequestBody List<Role> roleMenuRight, HttpServletRequest request) {
        roleMenuRightService.modifyRoleMenuRight(roleMenuRight);
        return ReturnBody.success();
    }

    @GetMapping(value = "rolemenuright/{roleId}")
    public ReturnBody getMenuRightByRoleId(@PathVariable("roleId") String roleId) {
        List<DRoleMenuTreeNode> menusByRole = roleMenuRightService.getRoleMenuRight(roleId);
        return ReturnBody.success().setData(menusByRole);
    }

    @PostMapping(value = "rolemenuright/{roleId}")
    public ReturnBody updateMenuRightByRoleid(@RequestBody List<DRoleMenuTreeNode> tree,@PathVariable("roleId") String roleId) {
        roleMenuRightService.modifyRoleMenuRight(tree,roleId);
        return ReturnBody.success();
    }

}
